# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=git
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
	 "${MINGW_PACKAGE_PREFIX}-${_realname}-doc-html"
	 "${MINGW_PACKAGE_PREFIX}-${_realname}-doc-man"
	 "${MINGW_PACKAGE_PREFIX}-${_realname}-test-artifacts")
tag=2.27.0.windows.1
pkgver=2.27.0.1.907ab1011d
pkgrel=1
pkgdesc="The fast distributed version control system (mingw-w64)"
arch=('any')
url="https://git-for-windows.github.io/"
license=('GPL2')

options=()
makedepends=('python2' 'less' 'openssh' 'patch' 'make' 'tar' 'diffutils'
	'ca-certificates' 'asciidoc' 'xmlto'
	"${MINGW_PACKAGE_PREFIX}-asciidoctor-extensions")
install=git.install

case "$(printf "%s\n%s\n" "$pkgver" "2.18.0" | sort -V)" in
2.18.0*) pcre="${MINGW_PACKAGE_PREFIX}-pcre2";;
*) pcre="${MINGW_PACKAGE_PREFIX}-pcre";;
esac

depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-ca-certificates"
         "${MINGW_PACKAGE_PREFIX}-expat>=2.0"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "$pcre"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "perl-Error"
         "perl>=5.14.0"
         "perl-Authen-SASL"
         "perl-libwww"
         "perl-MIME-tools"
         "perl-Net-SMTP-SSL"
         "perl-TermReadKey")

optdepends=("mintty")

source=("${_realname}"::"git+https://github.com/git-for-windows/git.git#tag=v$tag"
	'git-for-windows.ico'
	'start-ssh-agent.cmd'
	'start-ssh-pageant.cmd'
	'git-wrapper.c'
	'git-bash.rc'
	'git-bash.txt'
	'git-cmd.rc'
	'git-wrapper.rc'
	'gitk.rc'
	'compat-bash.rc'
	'mingw-w64-git.mak'
	'cv2pdb-strip'
	'edit-git-bash.c')

sha256sums=('SKIP'
            '62f455747f33953a723ff7b070d43f0ed03c1d8d0749d7bb06288e14d356b637'
            'f16b345aba17acd124ab5940635dfa2d87445df73eedbeb80e0285f29c85415a'
            '80b0b11efe5a2f9b4cd92f28c260d0b3aad8b809c34ed95237c59b73e08ade0b'
            '6696add21808e35ac171567e0286867ca9da8722c853af1295268869db84a602'
            'ee0ed30b2601413a6efcd15f236747404cd2d576dcfad77fb5b1700852cde857'
            'c975292adae1f2666f07f8ee9b7d50576da249d9151c6bd211602adc8d37b6ab'
            'a7e80489af33468e67a39d4c9c2fbf396e1f1e9533bb447f26432951ccf5e77c'
            '0726d386e98d703d5254458056e02824fb3de5602e55a31ec0cddf67c302c3a4'
            '0726d386e98d703d5254458056e02824fb3de5602e55a31ec0cddf67c302c3a4'
            'cbed8b133eb9eec9972f146be5c3ff49db29b2fff8ab9c87a6d0c646c08a5128'
            '595c3af0b17de8e0b042531d716448ecf4b27bd5d36a8ac46829d05ce4d2c9d5'
            '79bb4745063bc3e67831da1f694be0e98998b653a3e334e24db57500b1fee108'
            '6d83e1cb1acdb6eb1f2d5cb9299298e57680f5ca43d43c3e67c9da17f21b9b01')

if test -z "$WITHOUT_PDBS"
then
	pkgname+=("${MINGW_PACKAGE_PREFIX}-${_realname}-pdb")
	makedepends+=("${MINGW_PACKAGE_PREFIX}-cv2pdb")
	options+=('!strip')
else
	options+=('strip')
fi

pkgver() {
  cd "$srcdir/git"
  basever=${tag%.windows.*}
  test "$tag" = "$basever" ||
  basever="$basever.${tag##*.windows.}"
  basever="${basever/-/.}"
  printf "%s.%s" "${basever}" "$(git rev-parse --short HEAD)"
}

case "$CARCH" in
i686)
  SHAREDIR=/mingw32/share/git
  ;;
x86_64)
  SHAREDIR=/mingw64/share/git
  ;;
esac

prepare () {
	cd "$srcdir/git" &&

	cat >config.mak <<-EOF
	USE_ASCIIDOCTOR = YesPlease
	EOF

	cp ../git-bash.txt Documentation/

	# Slightly dirty workaround for missing docbook-xsl-ns
	grep -q http://docbook.sourceforge.net/release/xsl-ns/current /etc/xml/catalog || {
		(cd /usr/share/xml/ &&
		 test -d docbook-xsl-ns-1.78.1 ||
		 curl -L https://sourceforge.net/projects/docbook/files/docbook-xsl-ns/1.78.1/docbook-xsl-ns-1.78.1.tar.bz2/download |
		 tar xjf -)
		for rewrite in System URI
		do
			xmlcatalog --noout --add "rewrite$rewrite" \
				http://docbook.sourceforge.net/release/xsl-ns/current \
				/usr/share/xml/docbook-xsl-ns-1.78.1 \
				/etc/xml/catalog
		done
	}
}

build() {
  test -z "$PARALLEL_BUILD" || {
    echo "WARNING: PARALLEL_BUILD is deprecated. Use MAKEFLAGS=-j15 instead" >&2
    export MAKEFLAGS="$PARALLEL_BUILD $MAKEFLAGS"
  }
  export PYTHON_PATH=/usr/bin/python2
  cd "$srcdir/git"

  if test false != "$(git config core.autocrlf)"
  then
    git config core.autocrlf false
    rm .git/index
    git reset --hard
  fi

  # Git wants to decide itself whether to use ANSI stdio emulation or not
  CPPFLAGS="$(echo "$CPPFLAGS" |
	sed 's/-D__USE_MINGW_ANSI_STDIO\(=[0-9]*\)\?//')"

  # Make git-bash.exe first, to avoid triggering `* new link flags`
  rm -f *.res &&
  make -f ../mingw-w64-git.mak git-wrapper.exe \
	git-bash.exe git-cmd.exe compat-bash.exe \
	cmd/git.exe cmd/gitk.exe cmd/git-gui.exe \
	edit-git-bash.exe &&
  rm -f *.res &&

  # Also build git-credential-wincred.exe before the rest, so that those
  # LDFLAGS *really* do not cause `git.exe` to be re-linked after it was
  # stripped.
  make -C contrib/credential/wincred clean all &&
  CC=gcc make -C contrib/credential/wincred &&

  targets=
  case "${pkgname[@]}" in ''|*-git|*-git\ *) targets="$targets all";; esac
  case "${pkgname[@]}" in ''|*html*) targets="$targets html";; esac
  case "${pkgname[@]}" in ''|*man*) targets="$targets man";; esac

  counter=0
  while ! make $targets
  do
    counter=$(($counter+1))
    test $counter -lt 5 || {
      echo "Could not build (tried 5 times)" >&2
      exit 1
    }
  done &&

  case "${pkgname[@]}" in *pdb*)
    make -f ../mingw-w64-git.mak STRIP=$PWD/../cv2pdb-strip STRIP_OPTS= strip-all;;
  esac &&
  case " $targets " in
  *" all "*)
    make -f ../mingw-w64-git.mak sign-executables &&
    make -f ../mingw-w64-git.mak print-builtins | tr ' ' '\n' > builtins.txt
    ;;
  esac
}

package_git () {
  export PYTHON_PATH=/usr/bin/python2
  cd "$srcdir"/git

  make -j1 DESTDIR="$pkgdir" install

  # completions
  PREFIX="$(expr "$(cat < GIT-PREFIX)" : '[^:]*:[^:]*:[^:]*:\([^:]*\):[^:]*')"
  install -d "$pkgdir/$PREFIX/share/git/completion/"
  install contrib/completion/* "$pkgdir/$PREFIX/share/git/completion/"

  # subtree, for backwards-compatibility with MSys2's Git package
  make -C contrib/subtree prefix="$pkgdir/$PREFIX" install

  # git-credential-wincred.exe
  make -C contrib/credential/wincred prefix="$pkgdir/$PREFIX" install

  # Git Bash, Git CMD
  install -d -m755 $pkgdir$SHAREDIR
  install -m755 ../git-for-windows.ico $pkgdir$SHAREDIR
  install -m755 git-bash.exe git-cmd.exe $pkgdir
  install -m755 edit-git-bash.exe $pkgdir$SHAREDIR

  # Compatibility Bash, git-wrapper.exe
  install -m755 compat-bash.exe git-wrapper.exe $pkgdir$SHAREDIR

  # Install Git wrapper into /cmd/
  install -d -m755 $pkgdir/cmd
  install -m755 cmd/git.exe cmd/gitk.exe cmd/git-gui.exe \
	../start-ssh-agent.cmd ../start-ssh-pageant.cmd \
	$pkgdir/cmd

  # Install builtins.txt
  install -m644 builtins.txt $pkgdir$SHAREDIR
}

package_git-doc-html () {
  cd "$srcdir"/git

  make DESTDIR="$pkgdir" install-html

  # subtree, for backwards-compatibility with MSys2's Git package
  make -C contrib/subtree prefix="$pkgdir/$PREFIX" install-html
}

package_git-doc-man () {
  cd "$srcdir"/git

  make DESTDIR="$pkgdir" install-man

  # subtree, for backwards-compatibility with MSys2's Git package
  make -C contrib/subtree prefix="$pkgdir/$PREFIX" install-man
}

package_git-test-artifacts () {
  cd "$srcdir"/git

  make -f ../mingw-w64-git.mak DESTDIR="$pkgdir" install-mingit-busybox-test-artifacts
}

package_git-pdb () {
  cd "$srcdir"/git

  make -f ../mingw-w64-git.mak DESTDIR="$pkgdir" install-pdbs
}

package_mingw-w64-i686-git () {
  package_git
}

package_mingw-w64-i686-git-doc-html () {
  package_git-doc-html
}

package_mingw-w64-i686-git-doc-man () {
  package_git-doc-man
}

package_mingw-w64-i686-git-test-artifacts () {
  package_git-test-artifacts
}

package_mingw-w64-i686-git-pdb () {
	package_git-pdb
}

package_mingw-w64-x86_64-git () {
  package_git
}

package_mingw-w64-x86_64-git-doc-html () {
  package_git-doc-html
}

package_mingw-w64-x86_64-git-doc-man () {
  package_git-doc-man
}

package_mingw-w64-x86_64-git-test-artifacts () {
  package_git-test-artifacts
}

package_mingw-w64-x86_64-git-pdb () {
	package_git-pdb
}
